% random sampling and animation generation
% for dynamic phase screen
close all;
% 系数随机采样
Gt = temporal_sampling(W_aj,params,C);
% rearrange
B_pre = zeros(size(Gt,3),size(Gt,2));
for i = 1:size(Gt,3)
    B_pre(i,:) = Gt(:,:,i);
end

% 动态相位屏的可视化
%-------------------------------------------
figure;
H = pcolor(x,x,ph(:,:,1)); shading interp %二维平面图
hc = colorbar; 
%colormap('hot'); % 选择可视化色彩
% 固定色彩映射范围
ca_limit = max(max(abs(ph)));
%ca_limit = 30;
caxis([-1*ca_limit,1*ca_limit]);
title(hc, 'Phase(rad)')
%set(hc,'Limits',[-10,10])
set(gcf,'doublebuffer','on'); % 设置图形属性 图形双缓冲技术
xlabel('单击空格停止动画页面','FontSize',12,'Color','r');
title('Phase Screen');
k = 1;
t = 0;
while k
    s = get(gcf,'currentkey');
    if strcmp(s,'space')
        clc;
        k = 0;
    end
    t = t+1;
    B_pre_temp = B_pre(:,t);
    pause(0.02); % 
    ph(idx) = K_L*B_pre_temp;

    t;
    set(H,'CData',ph);

% end of the 'While' loop
end
